import http from '@/utils/request'
import store, { AppDispatch } from '..'
import { ArticleAction } from '../reducer/article'

export const getArticles =
  ({ channel_id = 0, timestamp = Date.now(), isRefresh = false }) =>
  async (dispatch: AppDispatch) => {
    const res = await http.get('/v1_0/articles', {
      params: { channel_id, timestamp },
    })
    console.log(res)

    const { articles } = store.getState().article

    const list = articles[channel_id]?.list

    const pre_timestamp = res.data.pre_timestamp

    if (!list || isRefresh) {
      dispatch({
        type: 'article/saveArticles',
        payload: {
          ...articles,
          [channel_id]: {
            timestamp: pre_timestamp,
            list: res.data.results,
          },
        },
      })
    } else {
      dispatch({
        type: 'article/saveArticles',
        payload: {
          ...articles,
          [channel_id]: {
            timestamp: pre_timestamp,
            list: [...list, ...res.data.results],
          },
        },
      })
    }
    if (!pre_timestamp) return Promise.reject('没数据')
  }

export const setArticleMark = ({
  visible = false,
  article_id = '',
}): ArticleAction => ({
  type: 'article/setMark',
  payload: { visible, article_id },
})

export const setArticleDislikes =
  (target: string) => async (dispatch: AppDispatch) => {
    const res = await http.post('/v1_0/article/dislikes', { target })
    console.log(res)

    const {
      channel: { activeId },
      article: { articles },
    } = store.getState()

    const { list, timestamp } = articles[activeId]

    dispatch({
      type: 'article/saveArticles',
      payload: {
        ...articles,
        [activeId]: {
          timestamp,
          list: list.filter((item) => item.art_id !== target),
        },
      },
    })
  }

export const setArticleReports =
  (target: string, type: string, remark = '') =>
  async (dispatch: AppDispatch) => {
    const res = await http.post('/v1_0/article/reports', {
      target,
      type,
      remark,
    })
    console.log(res)

    const {
      channel: { activeId },
      article: { articles },
    } = store.getState()

    const { list, timestamp } = articles[activeId]

    dispatch({
      type: 'article/saveArticles',
      payload: {
        ...articles,
        [activeId]: {
          timestamp,
          list: list.filter((item) => item.art_id !== target),
        },
      },
    })
  }

export const getArticleDetail =
  (article_id: string) => async (dispatch: AppDispatch) => {
    const res = await http.get('/v1_0/articles/' + article_id)
    console.log(res)
    dispatch({ type: 'article/saveDetail', payload: res.data })
  }

///v1_0/article/likings/:target
export const changeArticleLikings =
  (target: string) => async (dispatch: AppDispatch) => {
    const { detail } = store.getState().article

    if (detail.attitude !== 1) {
      const res = await http.post('/v1_0/article/likings', { target })
      console.log(res)
      dispatch({
        type: 'article/saveDetail',
        payload: { ...detail, attitude: 1 },
      })
    } else {
      const res = await http.delete('/v1_0/article/likings/' + target)
      console.log(res)
      dispatch({
        type: 'article/saveDetail',
        payload: { ...detail, attitude: -1 },
      })
    }
  }
